package com.vtc.session13;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SQLAdapter {
	private SQLHelper helper;
	private SQLiteDatabase db;
	
	public SQLAdapter(Context context){
	     helper = new SQLHelper(context, "test.db", null, 1);
	     db   = helper.getWritableDatabase();
	}
	public List<Student> fetchALl(){
		List<Student> list = new ArrayList<Student>();
		Cursor cursor =db.query("tblstudent", new String[]{"id","name","addr","age"}, null, null,null, null, null);
		while(cursor.moveToNext()){			
			Student st = new Student();
			st.setId(cursor.getInt(0));
			st.setName(cursor.getString(1));
			st.setAddr(cursor.getString(2));
			st.setAge(cursor.getInt(3));
			list.add(st);
		}
		return list;
	}
	public void add(Student st){
		ContentValues val = new ContentValues();
		val.put("name", st.getName());
		val.put("addr", st.getAddr());
		val.put("age", st.getAge());
		db.insert("tblstudent", null, val);		
	}
	public void update(Student st){
		ContentValues val = new ContentValues();
		val.put("name", st.getName());
		val.put("addr", st.getAddr());
		val.put("age", st.getAge());
		val.put("id", st.getId());
		db.update("tblstudent", val, " id="+st.getId(), null);
	}
	public void delete(int id){
		db.delete("tblstudent",  " id="+id,null);
	}
	
}
